<?php

$pgheader = "Account munnau.com";
//include('Connect/_nconnect.php');
include('Connect/_connect.php');

if(isset($_GET['em_key'])){
	$emkey = $_GET['em_key'];
	$email = $_GET['email'];
	$check_key = mysql_query("SELECT * FROM muemail_auth WHERE emailkey = '$emkey'");
	if($check_key){
		$row_data = mysql_fetch_array($check_key);
		$email = $row_data['email'];
		$row_id = $row_data['id'];
	}else{
		header('location: error.php');
	}
}
?>
<?php include("Styles/_pageHeader.php"); ?>
	<div class="cont_size_def cont_body">
    	<div class="account_cont_form">
            <h1>Sign In</h1>
            <span class="errorMsg" id="loginError"></span>
            <hr color="#FFFFFF">
            <span id="LogSign_error"></span>
            <div id="logginin_cont"><img src="images/signingIn.gif" /></div>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="signin_form">
                <table>
                <div class="account_form_body">
                    <div id="signup_form">
                        <tr>
                        <td><div class="sif_txt_fld">Username:</div></td>
                        <td>
                            <input type="text" class="sif_input" name="username" id="emailText"/>
                        </td>
                        <td><span class="form_error" id="emailMsg"></span></td>
                        </tr>
                        <tr>
                        <td><div class="sif_txt_fld">Password:</div></td>
                        <td>
                            <input type="password" class="sif_input" name="password" id="passText"/>
                        </td>
                        <td><span class="form_error" id="passMsg"></span></td>
                        </tr>
                        <tr>
                        <td></td>
                        <td>
                        	<input id="loginBtn" class="account_btn" type="submit" name="login" value="LogIn" />
                        </td>
                        </tr>
                    </div>
                </div>
                </table>
            </form>
            <hr color="#FFFFFF">
            <br/>
        </div>
        <div class="account_cont_form">
            <h1>Do not have an Account Yet ?? Then Sign UP</h1>
            <hr color="#FFFFFF">
            <form method="post" id="signup_form">
                <table>
                <div class="account_form_body">
                    <div id="signup_form">
                        <tr>
                        <td><div class="sif_txt_fld">Email Address:</div></td>
                        <td>
                            <input type="text" class="sif_input" name="reg_email" id="firstEmailtxt"/>
                        </td>
                        <td><span class="form_error" id="firstEmailmsg"></span></td>
                        </tr>
                        <tr>
                        <td><div class="sif_txt_fld">Re-enter Email:</div></td>
                        <td>
                            <input type="text" class="sif_input" name="re_reg_email" id="secendEmailtxt"/>
                        </td>
                        <td><span class="form_error" id="secondEmailmsg"></span></td>
                        </tr>
                        <tr>
                        <td></td>
                        <td>
                        	<input id="signupBtn" class="account_btn" type="submit" name="signup" value="SignUp" />
                        </td>
                        </tr>
                    </div>
                </div>
                </table>
            </form>
            <hr color="#FFFFFF">
            <br/>
        </div>
    </div>

<script type="text/javascript">
	$(document).ready(function() {
		/*login control*/
		$("#loginBtn").on('click',function(){
			var email = $("#emailText").val();
			var pass = $("#passText").val();
			$("#LogSign_error").html("");
			var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if( $.trim(email) == '' && $.trim(pass) == ''){
				$("#emailMsg").text("Input your Email Address");
				$("#emailText").addClass("errorTextbx").focus();
				$("#passMsg").text("Input your password");
				$("#passText").addClass("errorTextbx");
			}else if($.trim(email) == ''){
				$("#emailMsg").text("Input your Email Address");
				$("#emailText").addClass("errorTextbx").focus();
				if($("#passText").hasClass("errorTextbx") == true){
					$("#passText").removeClass("errorTextbx");
					$("#passMsg").text("");	
				}
			}else {
				if(emailReg.test(email) == false){
					$("#emailMsg").text("Invalid email address");
					$("#emailText").addClass("errorTextbx").focus();
				}else{
					$("#emailMsg").text("");
					$("#emailText").removeClass("errorTextbx");
					if($.trim(pass) == ''){
						$("#passMsg").text("Input your password");
						$("#passText").addClass("errorTextbx").focus();
					}else{
						$("#passMsg").text("");
						if($("#passText").hasClass("errorTextbx")==true){$("#passText").removeClass("errorTextbx");}
						$("#LogSign_cont").hide();
						$("#logginin_cont").show();
						$.ajax({
							type: "POST",
							url: "Account/_proAcct.php",
							data: {email: email, pass: pass},
							success: function(msg){
								if($.trim(msg) != 'redirect'){
									//data = msg.split('++~');
									//var msg_alert = data[1] + ' ' + data[0];
									//var url = data[2];
									//$("#LogSign_cont").hide();
									$("#logginin_cont").hide();
									//$("#LogSign_error").html("");
									$("#loginError").html(msg);
									//$("#acct_siteInfo_cont").html("<div  class='user_details'>Hello <br/>"  + msg_alert + "</div>");
									
								}else{
									//var msg_alert = msg;
									//$("#LogSign_cont").show();
									//$("#logginin_cont").hide();
									//$("#LogSign_error").html(msg);
									//$("#loginUpForm").show();	
									window.location.replace('mainwall.php');
								}
							},
							error: function(){
									$("#LogSign_cont").show();
									$("#logginin_cont").hide();
									$("#LogSign_error").html("An error occured" + '<br/>' + "Please try again");
									//$("#loginUpForm").show();
								}
							});
						
					}
				}
			}
			
			return false;
		});
		/*signup control*/
        $("#signupBtn").on('click', function(e){
		 	var f_email = $("#firstEmailtxt").val();
		 	var s_email = $("#secondEmailtxt").val();
			$("#LogSign_error").html("");
		 	if($.trim(f_email) == ''){
			 	$("#firstEmailmsg").text('Input Your Email Address');
				$("#firstEmailtxt").addClass("errorTextbx").focus();
			 }else{
				 var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				 if(emailReg.test(f_email) == false){
					 $("#firstEmailmsg").text('Input A valid Email Address');
					 if($("#firstEmailtxt").hasClass("errorTextbx")==false){$("#firstEmailtxt").addClass("errorTextbx").focus()};
				 }else{
					 $("#firstEmailmsg").text('');
					if($("#firstEmailtxt").hasClass("errorTextbx")){$("#firstEmailtxt").removeClass("errorTextbx");}
					if($.trim(s_email) == ''){
						$("#secondEmailmsg").text('Re-enter you Email');
						$("#secondEmailtxt").addClass("errorTextbx").focus();
						}
					else if(s_email != f_email){
						$("#secondEmailmsg").text('Emails dont match');
						$("#secondEmailtxt").addClass("errorTextbx").focus();
					}else{
						$("#secondEmailmsg").text('');
						if($("#secondEmailtxt").hasClass("errorTextbx")){$("#secondEmailtxt").removeClass("errorTextbx").focus();}
						$("#LogSign_cont").hide();
						$("#logginin_cont").show();
						$.ajax({
						  type: "POST",
						  url: "_proAcct.php",
						  data: { f_email: f_email, s_email: s_email },
						  success: function( msg ) {
							if(msg.indexOf('++~') != -1){
								var data = msg.split('++~');
								var msg_alert = data[1] ;
								$("#LogSign_cont").show();
								$("#logginin_cont").hide();
								$("#LogSign_error").html(msg_alert);
								
							}else{
								var msg_alert = msg ;
								$("#LogSign_cont").hide();
								$("#logginin_cont").hide();
								$("#LogSign_error").html("<br/> Redirecting ...");
								window.location.replace(msg);
							}
							
						},
						error: function(ng,notgood){
							$("#LogSign_cont").show();
							$("#logginin_cont").hide();
							$("#LogSign_error").html("An error occured" + '<br/>' + "Please try again");
							$("#signUpForm").show();
							}
						});
					}
				 }
			 }
			 return false;
		});
    });
</script>
<?php include("Styles/_pageFooter.php") ?>